package com.nf.health.app.activity.my.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.nf.health.app.R;
import com.nf.health.app.activity.my.bean.MyHealthyTestCompeteModel;
import com.nf.health.app.activity.my.bean.QuestionResultModel;
import com.nf.health.app.adapter.ViewHolder;
import com.nf.health.app.customview.MyExpandableListView;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.LogUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/29/029.
 */

public class MyQuestionResultAdapter extends BaseExpandableListAdapter {
    public  List<QuestionResultModel.ListBean>  list;
    private Context context;

    public MyQuestionResultAdapter(List<QuestionResultModel.ListBean> list, Context context){
        this.context = context;
        this.list = list;
    }
    @Override
    public int getGroupCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
       Map<Integer, String> answers = list.get(groupPosition).getHashMap();
        return answers == null ? 0 : answers.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getHashMap();

    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        ViewHolder holder = ViewHolder.get(parent, convertView, R.layout.item_risk_evaluation2, groupPosition);
        holder.setText(R.id.tv_question_title, (groupPosition+1)+". "+list.get(groupPosition).getTitle());
        holder.getView(R.id.ll_titleSub).setClickable(false);
        if(list.get(groupPosition).getType().equals("1")){
            ((ImageView)holder.getView(R.id.iv)).setImageResource(R.drawable.iv_single_question);
        }else{
            ((ImageView)holder.getView(R.id.iv)).setImageResource(R.drawable.iv_multy_question);
        }

        return holder.getConvertView();
    }

    /**
     * 展开所有
     * @param lv_expandable
     */
    public void  expandGroupMyList(MyExpandableListView lv_expandable){
        for (int i = 0; i < list.size(); i++) {
            lv_expandable.expandGroup(i);
        }
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder holder = ViewHolder.get(parent, convertView, R.layout.item_evaluation_answer2, childPosition);
        CheckBox tv_title = holder.getView(R.id.tv_title2);
        Map<Integer, String> answerMap = (Map<Integer, String>) getChild(groupPosition, childPosition);
        QuestionResultModel.ListBean bean = list.get(groupPosition);
        Drawable drawable = null;
       for(Map.Entry<Integer, String> entry : answerMap.entrySet()){
           if(entry.getKey() == childPosition){
               if(bean.getType().equals("1")){//表示单选
                    String currentSelectAnswer =  bean.getChoose();
                   if(currentSelectAnswer.equals(entry.getValue()) && currentSelectAnswer.equals(bean.getCorrect())){//单选是正确的答案  selsect == current == true
                       drawable =   ContextCompat.getDrawable(context,R.drawable.iv_right_question);
                       drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                       tv_title.setButtonDrawable(drawable);
                   }else if(!currentSelectAnswer.equals(entry.getValue()) && entry.getValue().equals(bean.getCorrect())){//单选是错误的答案 selsect != current == true
                       drawable =   ContextCompat.getDrawable(context,R.drawable.iv_right_question);
                       drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                       tv_title.setButtonDrawable(drawable);
                   }else if(currentSelectAnswer.equals(entry.getValue()) && !entry.getValue().equals(bean.getCorrect())){//单选是错误的答案 selsect = current != true
                       drawable =   ContextCompat.getDrawable(context,R.drawable.iv_errot_question);
                       drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                       tv_title.setButtonDrawable(drawable);
                   }else if(!currentSelectAnswer.equals(entry.getValue()) && !entry.getValue().equals(bean.getCorrect())){//没有选择   selsect != current != true
                       drawable =   ContextCompat.getDrawable(context,R.drawable.iv_normble_question);
                       drawable.setBounds(0,0,DensityUtil.dip2px(context,10),DensityUtil.dip2px(context,10));
                       tv_title.setButtonDrawable(drawable);
                   }
               }else{//表示多选
                    String selectMultyAnswer = bean.getChoose();
                   if(selectMultyAnswer.contains(",")){//多选有多个答案
                        String [] answer = selectMultyAnswer.split(",");
                       for (int i = 0; i < answer.length; i++) {
                           if(answer[i] .equals(entry.getValue()) && bean.getCorrect().contains(entry.getValue())){//多选------项列是正确的   selsect == current == true
                               drawable =   ContextCompat.getDrawable(context,R.drawable.iv_right_question);
                               drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                               tv_title.setButtonDrawable(drawable);
                           }else if(answer[i] .equals(entry.getValue()) && !bean.getCorrect().contains(entry.getValue())) {//多选------项列是错误的 selsect = current != true
                               drawable =   ContextCompat.getDrawable(context,R.drawable.iv_errot_question);
                               drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                               tv_title.setButtonDrawable(drawable);
                           }else if(!answer[i] .equals(entry.getValue()) && bean.getCorrect().contains(entry.getValue())) {//多选------项列是错误的 selsect != current = true
                               drawable =   ContextCompat.getDrawable(context,R.drawable.iv_right_question);
                               drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                               tv_title.setButtonDrawable(drawable);
                           }else if(!answer[i] .equals(entry.getValue()) && !bean.getCorrect().contains(entry.getValue())) {//没有选择   selsect != current != true
                               drawable =   ContextCompat.getDrawable(context,R.drawable.iv_normble_question);
                               drawable.setBounds(0,0,DensityUtil.dip2px(context,10),DensityUtil.dip2px(context,10));
                               tv_title.setButtonDrawable(drawable);
                           }
                       }
                   }else{//多选有单个答案
                       if(bean.getCorrect().contains(bean.getChoose()) && bean.getChoose().equals(entry.getValue())) { // selsect == current == true
                           drawable = ContextCompat.getDrawable(context, R.drawable.iv_right_question);
                           drawable.setBounds(0, 0, DensityUtil.dip2px(context, 40), DensityUtil.dip2px(context, 40));
                           tv_title.setButtonDrawable(drawable);
                       }else if(!bean.getCorrect().contains(bean.getChoose()) && bean.getChoose().equals(entry.getValue())){ // selsect == current == true
                               drawable =   ContextCompat.getDrawable(context,R.drawable.iv_right_question);
                               drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                               tv_title.setButtonDrawable(drawable);
                       }else  if(bean.getCorrect().contains(bean.getChoose()) && !bean.getChoose().equals(entry.getValue())){ // selsect == current != true
                           drawable =   ContextCompat.getDrawable(context,R.drawable.iv_errot_question);
                           drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                           tv_title.setButtonDrawable(drawable);
                       }else  if(bean.getCorrect().contains(bean.getChoose()) && !bean.getChoose().equals(entry.getValue())) { // selsect != current != true
                           drawable =   ContextCompat.getDrawable(context,R.drawable.iv_normble_question);
                           drawable.setBounds(0,0,DensityUtil.dip2px(context,10),DensityUtil.dip2px(context,10));
                           tv_title.setButtonDrawable(drawable);
                       }
                   }
                    //没选择的仍然有正确和错误的
                   if(!selectMultyAnswer.contains(entry.getValue()) && bean.getCorrect().contains(entry.getValue())){//单选中没有选择，但是该选项是正确的答案 s != c = t
                       drawable =   ContextCompat.getDrawable(context,R.drawable.iv_right_question);
                       drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                       tv_title.setButtonDrawable(drawable);
                   }else if(!selectMultyAnswer.contains(entry.getValue()) && !bean.getCorrect().contains(entry.getValue())){//单选中没有选择，但是该选项是错误的答案  s != c != t
                       drawable =   ContextCompat.getDrawable(context,R.drawable.iv_normble_question);
                       drawable.setBounds(0,0,DensityUtil.dip2px(context,10),DensityUtil.dip2px(context,10));
                       tv_title.setButtonDrawable(drawable);
                   }
               }




               /*if(!bean.getChoose().equals(bean.getCorrect()) && !bean.getChoose().equals(entry.getValue())){
                  // tv_title.setButtonDrawable(R.drawable.iv_normble_question);
                   drawable =   ContextCompat.getDrawable(context,R.drawable.iv_normble_question);
                   drawable.setBounds(0,0,DensityUtil.dip2px(context,10),DensityUtil.dip2px(context,10));
                   tv_title.setButtonDrawable(drawable);
               }else if(bean.getChoose().equals(bean.getCorrect()) && bean.getChoose().equals(entry.getValue())){//正确的
                 //  tv_title.setButtonDrawable(R.drawable.iv_right_question);
                   drawable =   ContextCompat.getDrawable(context,R.drawable.iv_right_question);
                   drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                    tv_title.setButtonDrawable(drawable);
               }else if(!bean.getChoose().equals(bean.getCorrect()) && bean.getChoose().equals(entry.getValue())){////错误的
                //   tv_title.setButtonDrawable(R.drawable.iv_errot_question);
                   drawable =   ContextCompat.getDrawable(context,R.drawable.iv_errot_question);
                   drawable.setBounds(0,0,DensityUtil.dip2px(context,40),DensityUtil.dip2px(context,40));
                   tv_title.setButtonDrawable(drawable);
               }else{
                   drawable =   ContextCompat.getDrawable(context,R.drawable.iv_normble_question);
                   drawable.setBounds(0,0,DensityUtil.dip2px(context,10),DensityUtil.dip2px(context,10));
                    tv_title.setButtonDrawable(drawable);
               }*/
               tv_title.setText(entry.getValue());
                break;
           }
       }



        return holder.getConvertView();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
			/*
			 * 很重要：实现ChildView点击事件，必须返回true
			 * 子布局需要加：android:descendantFocusability="blocksDescendants"
			 */
        return true;
    }

}